//
//  ChatImageViewController.m
//  tcpoctest
//
//  Created by abesub on 13-2-27.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "ChatImageViewController.h"

@interface ChatImageViewController ()

@end

@implementation ChatImageViewController

@synthesize chatImg;

-(id)init
{
    self = [super init];
    if(self)
    {
        imgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        imgView.userInteractionEnabled = YES;
        imgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HIGTH)];
        imgScrollView.delegate = self;
        imgScrollView.showsVerticalScrollIndicator		= NO;
        imgScrollView.showsHorizontalScrollIndicator     = NO;
        imgScrollView.exclusiveTouch = YES;
        imgScrollView.userInteractionEnabled = YES;
        imgScrollView.multipleTouchEnabled = YES;
        imgScrollView.autoresizesSubviews = YES;
        [imgScrollView addSubview:imgView];
    }
    
    return self;
}

-(void)initUI
{
    [self createTitle];
    self.mtitle.text = @"聊天详情";
    self.view.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"保存" target:self action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    self.navBar.alpha = 0.5;
    [self.view insertSubview:imgScrollView belowSubview:self.navBar];
    
    UITapGestureRecognizer *tapRecognizer;
	tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tapRecognizer.numberOfTouchesRequired = 1;
    tapRecognizer.numberOfTapsRequired = 1;
	[imgView addGestureRecognizer:tapRecognizer];
}

-(void)viewWillShow
{
    self.navBar.alpha = 0.6;
    imgView.frame = CGRectMake((WINDOW_WIDTH - chatImg.size.width)/2,(WINDOW_HIGTH - chatImg.size.height)/2, chatImg.size.width, chatImg.size.height);
    imgView.image = chatImg;
    imgScrollView.frame = CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HIGTH);
    
    CGFloat redius;
    if(chatImg.size.height > imgScrollView.frame.size.height || chatImg.size.width > imgScrollView.frame.size.width)
    {
        if ((chatImg.size.width / imgScrollView.frame.size.width) < (chatImg.size.height / imgScrollView.frame.size.height)) {
            redius = imgScrollView.frame.size.height / chatImg.size.height;
        }else {
            redius = imgScrollView.frame.size.width / chatImg.size.width;
        }
    }else {
        redius = 1.0;
    }
    
    [imgScrollView setContentSize:chatImg.size];
    [imgScrollView setMinimumZoomScale:redius];
    [imgScrollView setZoomScale:[imgScrollView minimumZoomScale]];
    [imgScrollView setMaximumZoomScale:2.0];
    
    CGFloat offsetX = (imgScrollView.bounds.size.width > imgScrollView.contentSize.width)?
    (imgScrollView.bounds.size.width - imgScrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (imgScrollView.bounds.size.height > imgScrollView.contentSize.height)?
    (imgScrollView.bounds.size.height - imgScrollView.contentSize.height) * 0.5 : 0.0;
    imgView.center = CGPointMake(imgScrollView.contentSize.width * 0.5 + offsetX,
                                 imgScrollView.contentSize.height * 0.5 + offsetY);
}

#pragma mark -Private Fun -
-(void)backButtonPressed
{
    if(_isFullscreen){
        _isFullscreen = NO;
        [self exitFullscreen];
    }
    self.navBar.alpha = 1;
    [imgScrollView setZoomScale:1];
    [[ViewManager sharedViewManager] backView:self];
}

-(void)nextButtonPressed
{
  UIImageWriteToSavedPhotosAlbum(chatImg, self, nil, nil);  
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    if( _isFullscreen == NO ) {
		[self enterFullscreen];
	}
	else {
		[self exitFullscreen];
	}
}

#pragma mark -Fullscreen
- (void)enterFullscreen
{
	_isFullscreen = YES;
    
    CGRect navBarRect = self.navBar.frame;	
	[UIView beginAnimations:@"galleryOut" context:nil];
	[UIView setAnimationDelegate:self];
    self.navBar.frame = CGRectMake(navBarRect.origin.x, navBarRect.origin.y - navBarRect.size.height, navBarRect.size.width, navBarRect.size.height);
    imgScrollView.frame =CGRectMake(0, -20, WINDOW_WIDTH, WINDOW_HIGTH);
	[UIView commitAnimations];
}

- (void)exitFullscreen
{
    _isFullscreen = NO;    
    CGRect navBarRect = self.navBar.frame;
    
	[UIView beginAnimations:@"galleryIn" context:nil];
	[UIView setAnimationDelegate:self];
    self.navBar.frame = CGRectMake(navBarRect.origin.x, navBarRect.origin.y + navBarRect.size.height, navBarRect.size.width, navBarRect.size.height);
    imgScrollView.frame =CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HIGTH);
	[UIView commitAnimations];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UIScrollViewDelegate methods

#pragma mark -zoom
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return  imgView;
}

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView
{
    CGFloat offsetX = (imgScrollView.bounds.size.width > imgScrollView.contentSize.width)?
    (imgScrollView.bounds.size.width - imgScrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (imgScrollView.bounds.size.height > imgScrollView.contentSize.height)?
    (imgScrollView.bounds.size.height - imgScrollView.contentSize.height) * 0.5 : 0.0;
    imgView.center = CGPointMake(imgScrollView.contentSize.width * 0.5 + offsetX,
                                   imgScrollView.contentSize.height * 0.5 + offsetY);
}


@end
